package com.shazhu.nongjt.service.impl;

import com.shazhu.common.utils.DateUtils;
import com.shazhu.nongjt.domain.NjtPrinterDevice;
import com.shazhu.nongjt.mapper.NjtPrinterDeviceMapper;
import com.shazhu.nongjt.service.INjtPrinterDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/** 打印机设备Service业务层处理
 * 
 * @author shazhu
 * @date 2024-09-28
 */
@Service
public class NjtPrinterDeviceServiceImpl implements INjtPrinterDeviceService
{
    @Autowired
    private NjtPrinterDeviceMapper njtPrinterDeviceMapper;

    /**
     * 查询打印机设备
     * 
     * @param id 打印机设备主键
     * @return 打印机设备
     */
    @Override
    public NjtPrinterDevice selectNjtPrinterDeviceById(Long id)
    {
        return njtPrinterDeviceMapper.selectNjtPrinterDeviceById(id);
    }

    /**
     * 查询打印机设备列表
     * 
     * @param njtPrinterDevice 打印机设备
     * @return 打印机设备集合
     */
    @Override
    public List<NjtPrinterDevice> selectNjtPrinterDeviceList(NjtPrinterDevice njtPrinterDevice)
    {
        return njtPrinterDeviceMapper.selectNjtPrinterDeviceList(njtPrinterDevice);
    }

    /**
     * 新增打印机设备
     * 
     * @param njtPrinterDevice 打印机设备
     * @return 结果
     */
    @Override
    public int insertNjtPrinterDevice(NjtPrinterDevice njtPrinterDevice)
    {
        njtPrinterDevice.setCreateTime(DateUtils.getNowDate());
        return njtPrinterDeviceMapper.insertNjtPrinterDevice(njtPrinterDevice);
    }

    /**
     * 修改打印机设备
     * 
     * @param njtPrinterDevice 打印机设备
     * @return 结果
     */
    @Override
    public int updateNjtPrinterDevice(NjtPrinterDevice njtPrinterDevice)
    {
        njtPrinterDevice.setUpdateTime(DateUtils.getNowDate());
        return njtPrinterDeviceMapper.updateNjtPrinterDevice(njtPrinterDevice);
    }

    /**
     * 批量删除打印机设备
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    @Override
    public int deleteNjtPrinterDeviceByIds(Long[] ids)
    {
        return njtPrinterDeviceMapper.deleteNjtPrinterDeviceByIds(ids);
    }

    /**
     * 删除打印机设备信息
     * 
     * @param id 打印机设备主键
     * @return 结果
     */
    @Override
    public int deleteNjtPrinterDeviceById(Long id)
    {
        return njtPrinterDeviceMapper.deleteNjtPrinterDeviceById(id);
    }

    /**
     * 根据序列号查询
     */
    @Override
    public NjtPrinterDevice selectBySerialCode(String serialCode) {
        return njtPrinterDeviceMapper.selectBySerialCode(serialCode);
    }

    /**
     * 根据MAC地址查询
     */
    @Override
    public NjtPrinterDevice selectByMacCode(String macCode) {
        return njtPrinterDeviceMapper.selectByMacCode(macCode);
    }

    /**
     * 更新最后连接时间
     */
    @Override
    public int updateLastConnectTime(Long id) {
        NjtPrinterDevice printer = new NjtPrinterDevice();
        printer.setId(id);
        printer.setLastConnectTime(DateUtils.getNowDate());
        return njtPrinterDeviceMapper.updateNjtPrinterDevice(printer);
    }

    /**
     * 连接打印机
     */
    @Override
    public int linkPrinterByMac(NjtPrinterDevice njtPrinterDevice) {
        NjtPrinterDevice existingPrinter = njtPrinterDeviceMapper.selectByMacCode(njtPrinterDevice.getMacCode());
        
        if (existingPrinter != null) {
            // 更新现有记录
            existingPrinter.setLastConnectTime(DateUtils.getNowDate());
            existingPrinter.setBindUserId(njtPrinterDevice.getBindUserId());
            return njtPrinterDeviceMapper.updateNjtPrinterDevice(existingPrinter);
        } else {
            // 创建新记录
            njtPrinterDevice.setCreateTime(DateUtils.getNowDate());
            njtPrinterDevice.setAuthStatus(0); // 默认未认证
            return njtPrinterDeviceMapper.insertNjtPrinterDevice(njtPrinterDevice);
        }
    }
}